<template>
  <ex-dialog v-model="visible" draggable :title="form.currencyName ? form.currencyName : (form.id ? form.currencyName : '新增')"
  width="700" :contentHeight="450">
    <template #default>
      <el-tabs v-model="activeName"  style="height: 100%">
        <el-tab-pane label="基本信息" name="base" style="height: 100%">
          <el-row :gutter="80">
            <el-col :lg="24">
              <el-form :model="form" :rules="button_rules" ref="dialogForm" label-width="130px" label-currency="left">
                <el-form-item label="货币编码" prop="currencyCode">
                  <el-input v-model="form.currencyCode" clearable placeholder="货币编码"></el-input>
                </el-form-item>

                <el-form-item label="货币名称" prop="currencyName">
                  <el-input v-model="form.currencyName" clearable placeholder="货币名称"></el-input>
                </el-form-item>
                <el-form-item label="上级货币" prop="parentLevelCode">
                  <ex-cascader v-model="form.parentLevelCode" dict-code="FamsCurrency" key-prop="levelCode" placeholder="默认所有" ></ex-cascader>
<!--                  <el-cascader v-model="form.parentLevelCode" :options="currencyOptions" :props="currencyProps"-->
<!--                               :show-all-levels="false" placeholder="默认所有" clearable></el-cascader>-->
                </el-form-item>
                <el-form-item label="名称缩写" prop="currencyShortName">
                  <el-input v-model="form.currencyShortName" clearable placeholder="名称缩写"></el-input>
                </el-form-item>
                <el-form-item label="对人民币汇率" prop="rmbRate">
                  <el-input v-model="form.rmbRate" clearable placeholder="对人民币汇率"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="orderWeight">
                <el-input v-model="form.orderWeight" clearable placeholder="排序"></el-input>
                </el-form-item>

              </el-form>

            </el-col>
          </el-row>
        </el-tab-pane>

      </el-tabs>
    </template>
    <template #footer>
      <div style="flex: auto;text-align: center">
        <el-button plain type="primary" @click="save" :loading="loading">保 存</el-button>
        <el-button plain type="warning" @click="reset" :loading="loading">重 置</el-button>
        <el-button plain type="info" @click="this.visible = false" :loading="loading">关 闭</el-button>
      </div>
    </template>

  </ex-dialog>
</template>

<script>
import scIconSelect from '@/components/scIconSelect'
import ExRelTable from "@/components/exRelTable/index.vue";
import validare from "@/utils/verificate"
import ExCascader from "@/components/exCascader/index.vue";
import exDialog from '@/components/exDialog';
import BaseTemplate from "@/utils/baseTemplate";

export default {
  computed: {
  },
  emits: ['success'],
  components: {
    ExCascader,
    ExRelTable,
    scIconSelect,
    exDialog
  },
  props: {
    menu: {
      type: Object, default: () => {
      }
    },
  },
  data() {
    return {
      activeName: 'base',
      visible: false,
      form: {
        id: "",
        currencyCode: "",
        currencyName: "",
        currencyShortName: "",
        rmbRate: "",
        pathCode: "",
        levelCode: "",
      },
      currencyOptions: [],
      currencyProps: {
        value: 'levelCode',
        label: 'label',
        checkStrictly: true,
        emitPath:false,
      },
      button_rules: {
        currencyCode: [
          {required: true, message: "请输入货币编码", trigger: "blur"},
        ],
        currencyName: [
          {required: true, message: "请输入货币标题", trigger: "blur"},
        ],
        contactPhone: [
          {validator:validare.verifyPhone, trigger: "change"},
        ],
      },
      loading: false,
      oldForm: null,
    }
  },
  watch: {},
  mounted() {
  },
  onLoad() {
  },
  methods: {
    relHandler(relMap) {
      if (!this.form.relMaps) {
        this.form.relMaps = {};
      }
      for (let key in relMap) {//清空旧的数据
        this.form.relMaps[key] = {};
      }
      //重新赋值新数据
      Object.assign(this.form.relMaps, relMap);
    },
    //显示
    open(entity) {
      this.form = Object.assign({}, entity);
      this.oldForm = Object.assign({}, this.form);
      this.visible = true;
      this.activeName = "base";
      this.loadtree();
    },
    reset() {
      this.form = Object.assign({}, this.oldForm);
		for(let ref in this.$refs){
			try {
				this.$refs[ref].refresh();
			}catch (e){
			}
		}
    },
    async loadtree() {
      var res = await this.$API.system.currency.treeData.post();
      if (res.code === 2000) {

        this.currencyOptions = res.data;
        this.addDisabled(this.currencyOptions);
      } else {
        this.$message.warning("上级菜单加载失败，请稍后重新");
      }
    },
    addDisabled(datas){
      datas.forEach(item=>{

        item.disabled = item.isEnabled !== '1';
        if(item.children){
          this.addDisabled(item.children);
        }
      })
    },
    //保存
    async save() {
      let valid = await BaseTemplate.validate(this,this.$refs.dialogForm);
      if (valid) {
        try {
          this.loading = true
          var res;
          if (this.form.id) {
            res = await this.$API.system.currency.edit.post(this.form)
          } else {
            res = await this.$API.system.currency.add.post(this.form)
          }
          if (res.code === 2000) {

            this.$message.success(this.$t("code."+res.code));
			this.$emit('success');
            this.visible = false;
			this.$store.dispatch("commit",this.form.entityName);
          } else {
            this.$message.warning(res.message)
          }
        } finally {
          this.loading = false
        }
      }
    },
  }
}
</script>

<style scoped>
h2 {
  font-size: 17px;
  color: #3c4a54;
  padding: 0 0 30px 0;
}

[data-theme="dark"] h2 {
  color: #fff;
}

</style>
